import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Main {

    ReentrantLock lock = new ReentrantLock();
    Condition A = lock.newCondition();
    Condition B = lock.newCondition();
    Condition C = lock.newCondition();

    int contA = 1;
    int contB = 0;
    int contC = 0;

    public static void main(String[] args) {
        Main main = new Main();
        main.print();
    }
    public void print(){
        new Thread(()->{
            for (int i = 0; i<10; i++){
                try{
                    lock.lock();
                    if(contA == 0){
                        A.await();
                    }
                    System.out.print("{A,");
                    contA = 0;
                    contB = 1;
                    B.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }


            }
        }).start();

        new Thread(()->{
            for(int i = 0; i<10; i++){
                try{
                    lock.lock();
                    if(contB == 0){
                        B.await();
                    }
                    System.out.print("B,");
                    contB = 0;
                    contC = 1;
                    C.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }

            }
        }).start();
        new Thread(()->{
            for(int i = 0; i<10; i++){
                try{
                    lock.lock();
                    if(contC == 0){
                        C.await();
                    }
                    System.out.println("C,}");
                    contC = 0;
                    contA = 1;
                    A.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }

            }
        }).start();
    }
}